import * as apis from 'services/login'

const types = {
  GET_COUNTRY_SUCCESS: 'GET_COUNTRY_SUCCESS',
  GET_COUNTRY_FAILURE: ' GET_COUNTRY_FAILURE',
  SAVE_USERS_SUCCESS: 'SAVE_USERS_SUCCESS',
  USER_LOGOUT: 'USER_LOGOUT',
}

const state = {
  countryList: [],
  countryMobileList: [],
  userInfo: localStorage.userInfo
    ? JSON.parse(localStorage.userInfo)
    : {},
}

function flattenCountryList(array) {
  return array.reduce((acc, val) => acc.concat(val.list), [])
}

function getCountryMobile(array) {
  return array.map(item => ({
    value: item.countryMobile,
    label: `${item.countryString}(+${item.countryMobile})`,
  }))
}

const actions = {
  getCountry({ commit }) {
    return apis.getCountry()
      .then((res) => {
        if (res.code === 1000) {
          const countryList = flattenCountryList(res.data)
          commit(types.GET_COUNTRY_SUCCESS, countryList)
          return countryList
        }
        return []
      })
  },
  registerEmail({ commit }, payload) {
    return apis.registerEmail(payload)
      .then((res) => {
        if (res.code === 1000) {
          commit(types.SAVE_USERS_SUCCESS, res.data)
        }
        return res
      })
  },
  getVercode({ commit }, payload) {
    return apis.getVercode(payload)
      .then(res => res)
  },
  checkVercode({ commit }, payload) {
    return apis.checkVercode(payload)
      .then(res => res)
  },
  checkEmail({ commit }, payload) {
    return apis.checkEmail(payload)
      .then(res => res)
  },
  registerPhone({ commit }, payload) {
    return apis.registerPhone(payload)
      .then((res) => {
        if (res.code === 1000) {
          commit(types.SAVE_USERS_SUCCESS, res.data)
        }
        return res
      })
  },
  loginEmail({ commit }, payload) {
    return apis.loginEmail(payload)
      .then((res) => {
        if (res.code === 1000) {
          commit(types.SAVE_USERS_SUCCESS, res.data)
        }
        return res
      })
  },
  loginPhone({ commit }, payload) {
    return apis.loginPhone(payload)
      .then((res) => {
        if (res.code === 1000) {
          commit(types.SAVE_USERS_SUCCESS, res.data)
        }
        return res
      })
  },
  logout({ commit }) {
    commit(types.USER_LOGOUT)
  },
  thridLogin({ commit }, payload) {
    return apis.thridLogin(payload)
      .then((res) => {
        if (res.code === 1000) {
          commit(types.SAVE_USERS_SUCCESS, res.data)
        }
        return res
      })
  },
  thridBind({ commit }, payload) {
    return apis.thridBind(payload)
      .then((res) => {
        if (res.code === 1000) {
          commit(types.SAVE_USERS_SUCCESS, res.data)
        }
        return res
      })
  },
}

const mutations = {
  [types.GET_COUNTRY_SUCCESS](state, countryList) {
    state.countryList = countryList
    state.countryMobileList = getCountryMobile(countryList)
  },
  [types.SAVE_USERS_SUCCESS](state, data) {
    // 存localstorge
    try {
      localStorage.userInfo = JSON.stringify(data)
    } catch (err) {
      throw new Error(JSON.stringify(err))
    }
    state.userInfo = data
  },
  [types.USER_LOGOUT](state) {
    localStorage.userInfo = {}
    state.userInfo = {}
  },
}

export default {
  state,
  actions,
  mutations,
}
